gdal2Coordinates collection provides utilities for converting between different coordinate systems based on the geospatial information embedded in raster files.
Overview
These scripts read the coordinate system and geotransformation matrix from a GDAL-supported raster file to perform coordinate transformations. All conversions use the projection information from the input image.Available Scripts
pixel2longlat.py
Converts pixel coordinates to latitude/longitude (geographic) coordinates.float
required
The pixel/sample position (column number)
float
required
The line position (row number)
string
required
Path to the input raster file
The script calculates coordinates for the center of the specified pixel by applying a half-pixel shift.
pixel2meters.py
Converts pixel coordinates to projected coordinates in meters (or feet, depending on the projection).float
required
The pixel/sample position (column number)
float
required
The line position (row number)
string
required
Path to the input raster file
longlat2meters.py
Converts longitude/latitude coordinates to projected coordinates (X, Y) in meters or feet.float
required
Longitude in decimal degrees
float
required
Latitude in decimal degrees
string
required
Path to the input raster file (provides projection information)
meters2longlat.py
Converts projected coordinates (X, Y) in meters or feet to longitude/latitude.float
required
X coordinate in meters (or feet)
float
required
Y coordinate in meters (or feet)
string
required
Path to the input raster file (provides projection information)
Implementation Details
Coordinate Transformation Process
All scripts follow a similar workflow:1
Open the input raster
Use GDAL to open the raster file in read-only mode
2
Read geotransformation matrix
Extract the affine transformation coefficients that map between pixel and ground coordinates
3
Create spatial reference objects
Build OGR spatial reference objects from the raster’s projection information
4
Transform coordinates
Apply the appropriate transformation using GDAL/OGR coordinate transformation functions
5
Apply pixel center offset (where applicable)
Shift coordinates to represent the center of pixels rather than corners
Geotransformation Matrix
GDAL uses a 6-coefficient affine transformation:geomatrix[0]: Top-left X coordinategeomatrix[1]: Pixel width (X resolution)geomatrix[2]: Row rotation (typically 0)geomatrix[3]: Top-left Y coordinategeomatrix[4]: Column rotation (typically 0)geomatrix[5]: Pixel height (Y resolution, typically negative)
Requirements
- Modern:
from osgeo import gdal, osr - Legacy:
import gdal(automatically detected)
Use Cases
Interactive Mapping
Convert mouse clicks on images to geographic coordinates for display
Data Integration
Transform coordinates between datasets with different projections
Ground Control
Identify ground coordinates for surveyed pixel locations
Quality Control
Verify georeferencing accuracy at known control points
Credits
Based ontolatlong.py by Andrey Kiselev (dron@remotesensing.org). Extended for meter/feet conversions by Trent Hare (USGS).
These scripts are part of the GDAL Python samples and are released under the MIT License.